/*
 * This file is part of the openHiTLS project.
 *
 * openHiTLS is licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "hitls_build.h"
#ifdef HITLS_CRYPTO_PROVIDER

#include "crypt_eal_implprovider.h"
#include "crypt_dsa.h"
#include "crypt_curve25519.h"
#include "crypt_rsa.h"
#include "crypt_dh.h"
#include "crypt_ecdsa.h"
#include "crypt_ecdh.h"
#include "crypt_sm2.h"
#include "crypt_paillier.h"
#include "crypt_errno.h"
#include "bsl_log_internal.h"
#include "bsl_err_internal.h"

void *CRYPT_EAL_DefPkeyMgmtNewCtx(void *provCtx, int32_t algId)
{
    (void) provCtx;
    void *pkeyCtx = NULL;
    switch (algId) {
        case CRYPT_PKEY_DSA:
            pkeyCtx = CRYPT_DSA_NewCtx();
            break;
        case CRYPT_PKEY_ED25519:
            pkeyCtx = CRYPT_ED25519_NewCtx();
            break;
        case CRYPT_PKEY_X25519:
            pkeyCtx = CRYPT_X25519_NewCtx();
            break;
        case CRYPT_PKEY_RSA:
            pkeyCtx = CRYPT_RSA_NewCtx();
            break;
        case CRYPT_PKEY_DH:
            pkeyCtx = CRYPT_DH_NewCtx();
            break;
        case CRYPT_PKEY_ECDSA:
            pkeyCtx = CRYPT_ECDSA_NewCtx();
            break;
        case CRYPT_PKEY_ECDH:
            pkeyCtx = CRYPT_ECDH_NewCtx();
            break;
        case CRYPT_PKEY_SM2:
            pkeyCtx = CRYPT_SM2_NewCtx();
            break;
        case CRYPT_PKEY_PAILLIER:
            pkeyCtx = CRYPT_PAILLIER_NewCtx();
            break;
    }
    if (pkeyCtx == NULL) {
        BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT);
        return NULL;
    }
    return pkeyCtx;
};

const CRYPT_EAL_Func g_defKeyMgmtDsa[] = {
    {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, CRYPT_EAL_DefPkeyMgmtNewCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, CRYPT_DSA_SetPara},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPARAM, CRYPT_DSA_GetPara},
    {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, CRYPT_DSA_Gen},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, CRYPT_DSA_SetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, CRYPT_DSA_SetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, CRYPT_DSA_GetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, CRYPT_DSA_GetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, CRYPT_DSA_DupCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_CHECK, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, CRYPT_DSA_Cmp},
    {CRYPT_EAL_IMPLPKEYMGMT_COPYPARAM, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_CTRL, CRYPT_DSA_Ctrl},
    {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, CRYPT_DSA_FreeCtx},
    CRYPT_EAL_FUNC_END,
};

const CRYPT_EAL_Func g_defKeyMgmtEd25519[] = {
    {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, CRYPT_EAL_DefPkeyMgmtNewCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, CRYPT_ED25519_GenKey},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, CRYPT_CURVE25519_SetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, CRYPT_CURVE25519_SetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, CRYPT_CURVE25519_GetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, CRYPT_CURVE25519_GetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, CRYPT_CURVE25519_DupCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_CHECK, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, CRYPT_CURVE25519_Cmp},
    {CRYPT_EAL_IMPLPKEYMGMT_COPYPARAM, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_CTRL, CRYPT_CURVE25519_Ctrl},
    {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, CRYPT_CURVE25519_FreeCtx},
    CRYPT_EAL_FUNC_END,
};

const CRYPT_EAL_Func g_defKeyMgmtX25519[] = {
    {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, CRYPT_EAL_DefPkeyMgmtNewCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, CRYPT_X25519_GenKey},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, CRYPT_CURVE25519_SetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, CRYPT_CURVE25519_SetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, CRYPT_CURVE25519_GetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, CRYPT_CURVE25519_GetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, CRYPT_CURVE25519_DupCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_CHECK, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, CRYPT_CURVE25519_Cmp},
    {CRYPT_EAL_IMPLPKEYMGMT_COPYPARAM, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_CTRL, CRYPT_CURVE25519_Ctrl},
    {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, CRYPT_CURVE25519_FreeCtx},
    CRYPT_EAL_FUNC_END,
};

const CRYPT_EAL_Func g_defKeyMgmtRsa[] = {
    {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, CRYPT_EAL_DefPkeyMgmtNewCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, CRYPT_RSA_SetPara},
    {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, CRYPT_RSA_Gen},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, CRYPT_RSA_SetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, CRYPT_RSA_SetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, CRYPT_RSA_GetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, CRYPT_RSA_GetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, CRYPT_RSA_DupCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_CHECK, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, CRYPT_RSA_Cmp},
    {CRYPT_EAL_IMPLPKEYMGMT_COPYPARAM, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_CTRL, CRYPT_RSA_Ctrl},
    {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, CRYPT_RSA_FreeCtx},
    CRYPT_EAL_FUNC_END,
};

const CRYPT_EAL_Func g_defKeyMgmtDh[] = {
    {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, CRYPT_EAL_DefPkeyMgmtNewCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, CRYPT_DH_SetPara},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPARAM, CRYPT_DH_GetPara},
    {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, CRYPT_DH_Gen},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, CRYPT_DH_SetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, CRYPT_DH_SetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, CRYPT_DH_GetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, CRYPT_DH_GetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, CRYPT_DH_DupCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_CHECK, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, CRYPT_DH_Cmp},
    {CRYPT_EAL_IMPLPKEYMGMT_COPYPARAM, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_CTRL, CRYPT_DH_Ctrl},
    {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, CRYPT_DH_FreeCtx},
    CRYPT_EAL_FUNC_END,
};

const CRYPT_EAL_Func g_defKeyMgmtEcdsa[] = {
    {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, CRYPT_EAL_DefPkeyMgmtNewCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, CRYPT_ECDSA_SetPara},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPARAM, CRYPT_ECDSA_GetPara},
    {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, CRYPT_ECDSA_Gen},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, CRYPT_ECDSA_SetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, CRYPT_ECDSA_SetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, CRYPT_ECDSA_GetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, CRYPT_ECDSA_GetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, CRYPT_ECDSA_DupCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_CHECK, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, CRYPT_ECDSA_Cmp},
    {CRYPT_EAL_IMPLPKEYMGMT_COPYPARAM, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_CTRL, CRYPT_ECDSA_Ctrl},
    {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, CRYPT_ECDSA_FreeCtx},
    CRYPT_EAL_FUNC_END,
};

const CRYPT_EAL_Func g_defKeyMgmtEcdh[] = {
    {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, CRYPT_EAL_DefPkeyMgmtNewCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, CRYPT_ECDH_SetPara},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPARAM, CRYPT_ECDH_GetPara},
    {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, CRYPT_ECDH_Gen},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, CRYPT_ECDH_SetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, CRYPT_ECDH_SetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, CRYPT_ECDH_GetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, CRYPT_ECDH_GetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, CRYPT_ECDH_DupCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_CHECK, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, CRYPT_ECDH_Cmp},
    {CRYPT_EAL_IMPLPKEYMGMT_COPYPARAM, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_CTRL, CRYPT_ECDH_Ctrl},
    {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, CRYPT_ECDH_FreeCtx},
    CRYPT_EAL_FUNC_END,
};

const CRYPT_EAL_Func g_defKeyMgmtSm2[] = {
    {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, CRYPT_EAL_DefPkeyMgmtNewCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, CRYPT_SM2_Gen},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, CRYPT_SM2_SetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, CRYPT_SM2_SetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, CRYPT_SM2_GetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, CRYPT_SM2_GetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, CRYPT_SM2_DupCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_CHECK, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, CRYPT_SM2_Cmp},
    {CRYPT_EAL_IMPLPKEYMGMT_COPYPARAM, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_CTRL, CRYPT_SM2_Ctrl},
    {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, CRYPT_SM2_FreeCtx},
    CRYPT_EAL_FUNC_END,
};

const CRYPT_EAL_Func g_defKeyMgmtPaillier[] = {
    {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, CRYPT_EAL_DefPkeyMgmtNewCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, CRYPT_PAILLIER_SetPara},
    {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, CRYPT_PAILLIER_Gen},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, CRYPT_PAILLIER_SetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, CRYPT_PAILLIER_SetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, CRYPT_PAILLIER_GetPrvKey},
    {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, CRYPT_PAILLIER_GetPubKey},
    {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, CRYPT_PAILLIER_DupCtx},
    {CRYPT_EAL_IMPLPKEYMGMT_CHECK, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_COPYPARAM, NULL},
    {CRYPT_EAL_IMPLPKEYMGMT_CTRL, CRYPT_PAILLIER_Ctrl},
    {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, CRYPT_PAILLIER_FreeCtx},
    CRYPT_EAL_FUNC_END,
};

#endif /* HITLS_CRYPTO_PROVIDER */